package com.blb.demo;

/**
 * 饿汉式单例模式
 */
class HungerySingleton {

    //定义私有单例对象
    private static HungerySingleton instance = new HungerySingleton();

    //私有化构造方法
    private HungerySingleton(){}

    //提供能返回实例的方法
    public static HungerySingleton getInstance(){
        return instance;
    }
}

public class HungerySingletonDemo{
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            // 多线程环境下，创建实例
            new Thread(() -> {
                System.out.println(HungerySingleton.getInstance().hashCode());
            }).start();
        }
    }
}
